﻿//
// MainViewModel.cs
//
// Authors:
//    Anders Gade Nørskov <09549@iha.dk
//    Lars Rønde Hansen <09548@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;

namespace RSD.ViewModel
{
    using Data;
    using Messages;

    public class MainViewModel : ViewModelBase
    {
        private readonly RelayCommand aboutCommand;
        private User user;

        public MainViewModel()
        {
            Messenger.Default.Register<MainViewLoadedMessage>(this,
                msg =>
                {
                    this.User = msg.User;
                });

            aboutCommand = new RelayCommand(AboutExecuted);
        }

        public User User
        {
            get
            {
                return user;
            }    
            set
            {
                user = value;

                RaisePropertyChanged("User");
                RaisePropertyChanged("IsOperator");
                RaisePropertyChanged("IsProgrammer");
                RaisePropertyChanged("IsAdministrator");
            }
        }

        public bool IsOperator
        {
            get
            {
                if (user != null)
                {
                    return user.IsInRole("Operator");
                }

                return false;
            }
        }

        public bool IsProgrammer
        {
            get
            {
                if (user != null)
                {
                    return user.IsInRole("Programmer");
                }

                return false;
            }
        }

        public bool IsAdministrator
        {
            get
            {
                if (user != null)
                {
                    return user.IsInRole("Administrator");
                }

                return false;
            }
        }

        public ICommand AboutCommand
        {
            get
            {
                return aboutCommand;
            }
        }

        private void AboutExecuted()
        {
            Messenger.Default.Send(new OpenAboutViewMessage());
        }
    }
}
